﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class ChiNhanhDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {


            ChiNhanhDTO objectDTO = new ChiNhanhDTO();
            objectDTO.MaChiNhanh = int.Parse(dt.Rows[i]["MaChiNhanh"].ToString());
            objectDTO.TenChiNhanh = dt.Rows[i]["TenChiNhanh"].ToString();
            objectDTO.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            objectDTO.GhiChu = dt.Rows[i]["GhiChu"].ToString();
            return (object)objectDTO;
        }
        protected object GetDataFrom(DataTable dt, int i)
        {
            ChiNhanhDTO objectDTO = new ChiNhanhDTO();
            objectDTO.TenChiNhanh = dt.Rows[i]["TenChiNhanh"].ToString();
            return (object)objectDTO;
        }
        public String ViewChiTietTuyenDuong(int matuyen)
        {
            String strSQL = "Select MaDoan from LoTrinhTuyen where LoTrinhTuyen.MaTuyen='" + matuyen + "' order by STT ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);
            string chuoimadoan = "";
            if (dt.Rows.Count <= 0)
                return chuoimadoan;

            DataTable dt1 = new DataTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string sql = "select DiaDiemDau,DiaDiemCuoi from DoanDuong where MaDoan=" + int.Parse(dt.Rows[i][0].ToString()) + "";
                dt1 = new DataProvider().ExecuteQuery(sql);
                chuoimadoan += dt1.Rows[0][0].ToString() + ",";
            }
            chuoimadoan += dt1.Rows[0][1].ToString();
            string[] strS = chuoimadoan.Split(',');
            ChiNhanhDTO[] ChiNhanhDTOs = new ChiNhanhDTO[strS.Count()];
            string chuoichinhanh = "";
            for (int k = 0; k < strS.Count(); k++)
            {
                String sql1 = "select TenChiNhanh from CHINHANH where MaChiNhanh=" + int.Parse(strS[k].ToString()) + "";
                DataTable dt2 = new DataProvider().ExecuteQuery(sql1);
                // ChiNhanhDTOs[k] = (ChiNhanhDTO)GetDataFromDataRow(dt2, k);
                chuoichinhanh += dt2.Rows[0][0] + " -> ";
            }
            chuoichinhanh = chuoichinhanh.Substring(0, chuoichinhanh.Length - 4);

            return chuoichinhanh;

        }
        public ChiNhanhDTO[] GetAll()
        {
            String strSQL = "Select * from ChiNhanh ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            ChiNhanhDTO[] ChiNhanhDTOs = new ChiNhanhDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                ChiNhanhDTOs[i] = (ChiNhanhDTO)GetDataFromDataRow(dt, i);

            return ChiNhanhDTOs;
        }
        public DataTable GetAllDataTable()
        {
            String strSQL = "Select * from ChiNhanh ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);
            int d = dt.Rows.Count;
            return dt;
        }


        public bool insert(ChiNhanhDTO info)
        {
            string insertCommand = "INSERT CHINHANH VALUES(" +
                info.MaChiNhanh + ", '" +
                info.TenChiNhanh + ", '" +
                info.DiaChi + "', '" +
                info.GhiChu + "')";

            return ExecuteNonQuery(insertCommand);
        }

        public bool update(ChiNhanhDTO info)
        {
            string updateCommand = "UPDATE CHINHANH " +
                                    "SET TenChiNhanh = '" + info.TenChiNhanh + "', " +
                                    " DiaChi = '" + info.DiaChi + "', " +
                                    " GhiChu = '" + info.GhiChu + "'" +
                                    " WHERE MaKhachHang = " + info.MaChiNhanh;


            return ExecuteNonQuery(updateCommand);
        }

        public bool delete(int maChiNhanh)
        {
            string deleteCommand = "DELETE FROM CHINHANH WHERE MaChiNhanh = " + maChiNhanh;
            return ExecuteNonQuery(deleteCommand);
        }
    }
}
